<style scoped>
</style>
<!-- 添加外链接图片地址弹窗编辑器 -->
<template>
  <div class="edit-repertory-oname">
    <el-dialog @close="close" title="编辑资源名称" :close-on-click-modal="false" :visible.sync="centerDialogVisible"
      width="640px" center>
      <el-form ref="form" label-width="80px">
        <el-form-item label="资源名">
          <el-input size="small" v-model="oname" maxlength="100" style="width:450px;"></el-input>
          <span>{{'.' + suffix}}</span>
          <el-alert v-if="error" style="height: 30px;" :title="error" type="error">
          </el-alert>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="centerDialogVisible = false">取 消</el-button>
        <el-button :disabled="!(oname && !error)" size="small" type="primary" @click="ok">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    getName: {
      type: Function
    }
  },
  data () {
    return {
      centerDialogVisible: false,
      repertory: '',
      oname: '',
      error: '',
      suffix: ''
    }
  },
  methods: {
    close () {
      this.centerDialogVisible = false
      this.oname = ''
      this.error = ''
    },
    open (item) {
      this.centerDialogVisible = true
      this.repertory = item
      if (this.repertory.originalFilename && this.repertory.originalFilename.lastIndexOf('.') !== -1) {
        this.oname = this.repertory.originalFilename.substring(0, this.repertory.originalFilename.lastIndexOf('.'))
      } else {
        this.oname = this.repertory.originalFilename
      }
      this.suffix = this.repertory.suffix
    },
    ok () {
      if (!this.oname) {
        this.$message.error('资源名称不能为空！')
        return
      } else if (this.oname.length > 100) {
        this.$message.error('资源名称不能超过100个字！')
        return
      } else {
        let regx = /^[\u4e00-\u9fa50-9A-Za-z][\u4e00-\u9fa50-9A-Za-z@_.]*$/
        if (!regx.test(this.oname)) {
          this.$message.error('资源名称不能带特殊字符（除@_.外，且@_.不能作为开头）或空格！')
          return
        }
      }
      let newName
      if (this.suffix) {
        newName = this.oname + '.' + this.suffix
      } else {
        newName = this.oname
      }
      this.getName(this.repertory.id, newName, (rsp) => {
        if (rsp && rsp.id > 0) {
          this.centerDialogVisible = false
        }
      })
    }
  }
}

</script>
